package com.srt.service;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.srt.mapper.SrtContentMapper;
import com.srt.model.SrtContent;
import com.srt.util.SrtUtil;
import com.srt.util.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rd06
 * @since 2021-03-29
 */
@Service
@Slf4j
public class SrtContentService extends CommonService<SrtContentMapper, SrtContent> {
    @Autowired
    private CatalogService catalogService;

    public boolean uploadSrt(MultipartFile file, Long catalogId) {
        try (BufferedInputStream inputStream = new BufferedInputStream(file.getInputStream())) {
            byte[] bytes = new byte[1024];
            int num = 0;
            List<SrtContent> srtContentList = ListUtil.list(false);
            StringBuilder stringBuilder = new StringBuilder();
            while ((num = inputStream.read(bytes)) != -1) {
                stringBuilder.append(new String(bytes, 0, num));
            }
            inputStream.close();
            String content = stringBuilder.toString();
            String[] lines = content.split("\\r?\\n");
            SrtContent srtContent = SrtContent.builder().catalog(catalogId).isDelete(0).build();
            srtContentList.add(srtContent);
            int orderNum = 0;
            for (String line : lines) {
                if (orderNum == 0) {
                    srtContent.setOrderNum(Integer.parseInt(line));
                } else if (orderNum == 1) {
                    String[] timeS = line.split("-->");
                    String startTime = timeS[0].trim();
                    String endTime = timeS[1].trim();
                    srtContent.setStartTime(startTime.replaceAll(",", "."));
                    srtContent.setEndTime(endTime.replaceAll(",", "."));
                } else {
                    if (StrUtil.isNotEmpty(line)) {
                        if (SrtUtil.isContainChinese(line)) {
                            srtContent.setChContent(line);
                        } else {
                            srtContent.setEnContent(line);
                        }
                    }
                }
                orderNum++;
                if (StrUtil.isEmpty(line)) {
                    srtContent = SrtContent.builder().catalog(catalogId).isDelete(0).build();
                    orderNum = 0;
                    srtContentList.add(srtContent);
                }
            }
            this.saveBatch(srtContentList);
        } catch (IOException e) {
            log.error("上传文件异常了：", e);
            return false;
        }
        return true;
    }

    public List<SrtContent> getSrtContextList(Long catalogId) {
        return this.lambdaQuery().ge(SrtContent::getCatalog, catalogId).list();
    }

    /**
     * 批量更新
     *
     * @param srtContentList
     * @return
     */
    public boolean updateBatchSrtContent(List<SrtContent> srtContentList) {
        return this.updateBatchById(srtContentList);
    }
}
